package cn.core.dirs.rest;

import cn.core.dirs.eo.Files;
import cn.core.dirs.qo.FilesQO;
import cn.core.dirs.service.FilesService;
import cn.core.dirs.vo.FilesVO;
import cn.core.tool.exception.CoreException;
import cn.core.tool.rest.CoreController;
import cn.core.tool.util.CoreTool;
import cn.core.tool.util.Param;
import cn.core.tool.util.Res;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 文件 REST API
 */
@Log4j2
@RestController
@Scope("prototype")
@RequestMapping("dirs/files")
public class FilesController extends CoreController {

  public static final String name = "文件";

  @Resource
  private FilesService filesService;

  /**
   * 文件 >>> 保存
   * @param param 参数
   * @return 返回
   */
  @PostMapping(save)
  public Res<Files> save(@RequestBody Param<Files> param) throws Exception {
    Res<Files> res = new Res<>(name + succSave);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(filesService.save(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 文件 >>> 批量保存
   * @param param 参数
   * @return 返回
   */
  @PostMapping(saveBatch)
  public Res<Files> saveBatch(@RequestBody Param<Files> param) throws Exception {
    Res<Files> res = new Res<>(name + succSave);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(filesService.saveBatch(param.getData()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 文件 >>> 修改
   * @param param 参数
   * @return 返回
   */
  @PostMapping(update)
  public Res<Files> update(@RequestBody Param<Files> param) throws Exception {
    Res<Files> res = new Res<>(name + succUpdate);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(filesService.update(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 文件 >>> 批量修改
   * @param param 参数
   * @return 返回
   */
  @PostMapping(updateBatch)
  public Res<Files> updateBatch(@RequestBody Param<Files> param) throws Exception {
    Res<Files> res = new Res<>(name + succUpdate);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(filesService.updateBatch(param.getData()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 文件 >>> 删除
   * @param param 参数
   * @return 返回
   */
  @PostMapping(remove)
  public Res<Files> remove(@RequestBody Param<Files> param) throws Exception {
    Res<Files> res = new Res<>(name + succRemove);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      filesService.remove(param.get());
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 文件 >>> 批量删除
   * @param param 参数
   * @return 返回
   */
  @PostMapping(removeBatch)
  public Res<Files> removeBatch(@RequestBody Param<Files> param) throws Exception {
    Res<Files> res = new Res<>(name + succRemove);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      filesService.removeBatch(param.getData());
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 文件 >>> 数据量
   * @param param 参数
   * @return 返回
   */
  @PostMapping(count)
  public Res<Files> count(@RequestBody Param<FilesQO> param) throws Exception {
    Res<Files> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setCount(filesService.count(null));
      } else {
        res.setCount(filesService.count(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 文件 >>> 主键查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(pk)
  public Res<Files> pk(@RequestBody Param<FilesQO> param) throws Exception {
    Res<Files> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(filesService.pk(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 文件 >>> 查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(query)
  public Res<Files> query(@RequestBody Param<FilesQO> param) throws Exception {
    Res<Files> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setData(filesService.query(null));
      } else {
        res.setData(filesService.query(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 文件 >>> 分页
   * @param param 分页参数
   * @return 返回
   */
  @PostMapping(paging)
  public Res<Files> paging(@RequestBody Param<FilesQO> param) throws Exception {
    Res<Files> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(filesService.paging(param));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 文件 >>> 业务数据量
   * @param param 参数
   * @return 返回
   */
  @PostMapping(countVo)
  public Res<FilesVO> countVo(@RequestBody Param<FilesQO> param) throws Exception {
    Res<FilesVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setCount(filesService.countVo(null));
      } else {
        res.setCount(filesService.countVo(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 文件 >>> 业务主键查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(pkVo)
  public Res<FilesVO> pkVo(@RequestBody Param<FilesQO> param) throws Exception {
    Res<FilesVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(filesService.pkVo(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 文件 >>> 业务查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(queryVo)
  public Res<FilesVO> queryVo(@RequestBody Param<FilesQO> param) throws Exception {
    Res<FilesVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setData(filesService.queryVo(null));
      } else {
        res.setData(filesService.queryVo(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 文件 >>> 业务分页
   * @param param 分页参数
   * @return 返回
   */
  @PostMapping(pagingVo)
  public Res<FilesVO> pagingVo(@RequestBody Param<FilesQO> param) throws Exception {
    Res<FilesVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(filesService.pagingVo(param));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

}
